﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Janus.Windows.EditControls;

namespace UControls
{
    public partial class UCDaysOfWeek : UserControl
    {
        #region Properties
        private int _daysOfWeek = 0;
        public int DaysOfWeek
        {
            get { return _daysOfWeek; }
            set
            {
                if (_daysOfWeek != value)
                {
                    _daysOfWeek = value;
                    CheckValue(value);
                }
            }
        }
        #endregion

        #region Ctors
        public UCDaysOfWeek()
        {
            InitializeComponent();

            this.chkMon.CheckedChanged += new EventHandler(chkDays_CheckedChanged);
            this.chkTue.CheckedChanged += new EventHandler(chkDays_CheckedChanged);
            this.chkWed.CheckedChanged += new EventHandler(chkDays_CheckedChanged);
            this.chkThu.CheckedChanged += new EventHandler(chkDays_CheckedChanged);
            this.chkFri.CheckedChanged += new EventHandler(chkDays_CheckedChanged);
            this.chkSat.CheckedChanged += new EventHandler(chkDays_CheckedChanged);
            this.chkSun.CheckedChanged += new EventHandler(chkDays_CheckedChanged);
            
        }

        #endregion
    
        #region Events
        void chkDays_CheckedChanged(object sender, EventArgs e)
        {
            UICheckBox checkBox = sender as UICheckBox;
            int i = 0;
            switch (checkBox.Name.Substring(3))
            {
                case "Mon":
                    i = 1;
                    break;
                case "Tue":
                    i = 2;
                    break;
                case "Wed":
                    i = 4;
                    break;
                case "Thu":
                    i = 8;
                    break;
                case "Fri":
                    i = 16;
                    break;
                case "Sat":
                    i = 32;
                    break;
                case "Sun":
                    i = 64;
                    break;
            }

            if (checkBox.Checked)
                _daysOfWeek |= i;
            else
                _daysOfWeek &= -(i + 1);
        } 
        #endregion

        #region Methods
        public void CheckAll(bool IsChecked)
        {
            this.chkMon.Checked = IsChecked;
            this.chkTue.Checked = IsChecked;
            this.chkWed.Checked = IsChecked;
            this.chkThu.Checked = IsChecked;
            this.chkFri.Checked = IsChecked;
            this.chkSat.Checked = IsChecked;
            this.chkSun.Checked = IsChecked;
        }

        private void CheckValue(int value)
        {
            this.chkMon.Checked = (value & 1) == 1;
            this.chkTue.Checked = (value & 2) == 2;
            this.chkWed.Checked = (value & 4) == 4;
            this.chkThu.Checked = (value & 8) == 8;
            this.chkFri.Checked = (value & 16) == 16;
            this.chkSat.Checked = (value & 32) == 32;
            this.chkSun.Checked = (value & 64) == 64;
        }
        #endregion
    }
}
